package com.freelance.vn.apps.puzzle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.freelance.vn.apps.puzzle.R;
import com.freelance.vn.apps.puzzle.adapter.ImageAdapter;
import com.freelance.vn.apps.puzzle.model.Constants;
import com.freelance.vn.apps.puzzle.model.ImageIds;
import com.freelance.vn.apps.puzzle.view.CoverFlow;


import com.google.ads.AdRequest;
import com.google.ads.AdView;
	
public class MainScreen extends ActivityBase implements OnClickListener{
	private CoverFlow mCoverFlow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_screen);

//        ViewFlipper mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
//        mFlipper.startFlipping();
//        mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
//                android.R.anim.fade_in));
//        mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
//                android.R.anim.fade_out));
//        
		AdView adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());
		mCoverFlow = (CoverFlow) findViewById(R.id.myView);
		ImageAdapter coverImageAdapter = new ImageAdapter(this);
		mCoverFlow.setAdapter(coverImageAdapter);
		mCoverFlow.setSpacing(-60);
		mCoverFlow.setSelection(2, true);
		mCoverFlow.setAnimationDuration(1000);
		setupListeners(mCoverFlow);
		
		Button btnPlay =(Button)findViewById(R.id.btnPlay);
		btnPlay.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// play game
				startGame();
			}
		});

	}

	private void startGame(){
		Intent newGame = new Intent(this, PuzzleGameMainActivity.class);
		int imgId=mCoverFlow.getSelectedItemPosition();
		newGame.putExtra(Constants.EX_SELECTED_IMG, ImageIds.mImageIds[imgId]);
		Log.i("MainScreen","=>>selected Image :" + imgId);
		startActivity(newGame);
	}

	private void setupListeners(final CoverFlow mCoverFlow) {
		mCoverFlow.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(final AdapterView<?> parent,
					final View view, final int position, final long id) {
				startGame();
//				Toast.makeText(getApplicationContext(),
//						"Item clicked! : " + id, Toast.LENGTH_LONG).show();
			}

		});
		mCoverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(final AdapterView<?> parent,
					final View view, final int position, final long id) {
//				startGame();
			}

			@Override
			public void onNothingSelected(final AdapterView<?> parent) {
			}
		});
	}

	@Override
	public void onClick(View v) {
		if(v==null){
			return;
		}
		int id = (Integer)v.getId();
		switch(id){
		case R.id.btnBack:
		case R.id.btnLogo:
				Log.i("PuzzleGameMainActivity","on back");
				finish();
				break;
		case R.id.btnShare:
				share();
				Log.i("PuzzleGameMainActivity","on btn share");
				break;
		case R.id.btnAbout:
			Log.i("PuzzleGameMainActivity","on btn About");
			Intent intent = new Intent(getApplicationContext(), Instruction.class);
			startActivity(intent);
			break;
		default:
				break;
		
		}
		
	}
	private void share(){
		String appUrl="https://play.google.com/store/apps/details?id=com.freelance.vn.apps.puzzle";
//		appUrl="https://play.google.com/store/apps/details?id=com.handlerexploit.tweedle";
		Intent shareItent = new Intent(Intent.ACTION_SEND);
		shareItent.putExtra(Intent.EXTRA_TEXT, appUrl);
		shareItent.setType("text/plain");
		startActivity(Intent.createChooser(shareItent,"share to"));
		
		
	}

}
